﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Concurrency;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using PicturePortfolio.ServiceReference;
using Caliburn.Micro;
namespace PicturePortfolio.Silverlight.ViewModels
{
    public class PublicCommentsViewModel:PropertyChangedBase
    {
        PhotoServiceClient _service;
        Photo _photo;
        public PublicCommentsViewModel(PhotoServiceClient service,Photo photo)
        {
            _service = service;
            _photo = photo;
            this._individualComments = new ObservableCollection<IndividualCommentViewModel>();
            this.LoadComments(photo);
        }

        private ObservableCollection<IndividualCommentViewModel> _individualComments;
        public ObservableCollection<IndividualCommentViewModel> IndividualComments
        {
            get { return _individualComments; }
            set
            {
                _individualComments = value;
            }
        }
        public string UserName { get; set; }
        public string UserComment { get; set; }
        public void SaveComment()
        {
            PublicComment comment = new PublicComment();
            comment.Comment = this.UserComment;
            comment.UserName = this.UserName;
            
            //_service.AddCommentCompleted
            var func=Observable.FromEvent<System.ComponentModel.AsyncCompletedEventArgs>(_service,"AddCommentCompleted")
                .SubscribeOn(Scheduler.Dispatcher)
                .Select(r => r.EventArgs)
                .Subscribe(s => this.SaveCommentCompleted(s));

            _service.AddCommentAsync(comment, _photo);

            _individualComments.Clear();
            this.LoadComments(_photo);
            //func.SubscribeOn(Scheduler.Dispatcher)
            //    .Select(r => r.EventArgs)
            //    .Subscribe(s => this.SaveCommentCompleted(s));
        }
        private void SaveCommentCompleted(System.ComponentModel.AsyncCompletedEventArgs eventArgs)
        {
            if (eventArgs.Error == null)
            {
                //_individualComments.Clear();
                //this.LoadComments(_photo);
            }
            else
            {
                MessageBox.Show(eventArgs.Error.Message);
            }
        }
        private void LoadComments(Photo photo)
        {
            var func = Observable.FromEvent<GetCommentsByPhotoCompletedEventArgs>(_service, "GetCommentsByPhotoCompleted")
                .ObserveOn(Scheduler.ThreadPool);

            _service.GetCommentsByPhotoAsync(photo);

            func.Select(s => s.EventArgs.Result)
                .ObserveOn(Scheduler.Dispatcher)
                .Subscribe(r => this.ParsePhotoComments(r));
        }

        private void ParsePhotoComments(List<PublicComment> comments)
        {
            //_individualComments.Clear();
            foreach (PublicComment comment in comments)
            {
                IndividualCommentViewModel individualComment = new IndividualCommentViewModel(comment);
                _individualComments.Add(individualComment);
            }
        }


    }
}
